package com.cnfangmao.masterdata.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.utils.ModelUtils;
import com.cnfangmao.masterdata.model.EnumType;
import com.cnfangmao.masterdata.service.EnumTypeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 字段枚举类型 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Api(value = "字段枚举类型相关接口")
@RestController
@RequestMapping("/enumType")
public class EnumTypeController {

	@Autowired
	private EnumTypeService enumTypeService;

	/**
	 * 
	 * @param enumValueInfo
	 * @return
	 */
	@ApiOperation(value = "创建字段枚举类型", produces = "application/json")
	@ApiImplicitParam(name = "enumTypeInfo", value = "字段枚举类型信息", required = true, dataType = "Json", 
						example = "{\"typeCode\": \"typeCode1\", \"typeName\": \"typeName\"}")
	@PostMapping("/create.do")
	public ResBody create(String enumTypeInfo) {
		ResBody resBody = null;
		try {
			EnumType enumType = ModelUtils.parseObject(enumTypeInfo, EnumType.class);
			enumType.setCreatedBy("user11");
			enumType.setCreatedOn(new Date());
			enumType.setModifiedBy("user11");
			enumType.setModifiedOn(new Date());
			
			enumTypeService.save(enumType);

			resBody = ResBody.success();
		} catch (Exception e) {
			resBody = ResBody.failure().code(ResCodeEnum.PARAM_ERROR.getCode());
		}

		return resBody;
	}

	/**
	 * 
	 * @param enumValueInfo
	 * @return
	 */
	@ApiOperation(value = "更新字段枚举类型", produces = "application/json")
	@ApiImplicitParam(name = "enumTypeInfo", value = "字段枚举类型信息", required = true, dataType = "Json", 
						example = "{\"typeId\":\"typeId\", \"typeCode\": \"typeCode1\", \"typeName\": \"typeName\"}")
	@PostMapping("/update.do")
	public ResBody update(String enumTypeInfo) {
		ResBody resBody = null;
		try {
			EnumType enumType = JSONObject.parseObject(enumTypeInfo, EnumType.class);
			enumType.setModifiedBy("user11");
			enumType.setModifiedOn(new Date());
			enumTypeService.updateById(enumType);

			resBody = ResBody.success();
		} catch (Exception e) {
			resBody = ResBody.failure().code(ResCodeEnum.PARAM_ERROR.getCode());
		}

		return resBody;
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "获取字段枚举类型", produces = "application/json")
	@ApiImplicitParam(name = "id", value = "字段枚举类型的ID", required = true, dataType = "String")
	@GetMapping("/getById.do")
	public ResBody getById(String id) {
		ResBody resBody = null;
		try {
			EnumType enumType = enumTypeService.getById(id);

			resBody = ResBody.success(JSONObject.toJSON(enumType));
		} catch (Exception e) {
			resBody = ResBody.failure().code(ResCodeEnum.PARAM_ERROR.getCode());
		}
		return resBody;
	}

	/**
	 * 
	 * @param queryCondition
	 * @return
	 */
	@ApiOperation(value = "获取字段枚举类型列表", produces = "application/json")
	@ApiImplicitParam(name = "queryCondition", value = "查询条件(typeCode, typeName为like查询)", required = true, dataType = "Json", 
						example = "{\"typeId\":\"typeId\", \"typeCode\": \"typeCode1\", \"typeName\": \"typeName\"}")
	@PostMapping("/list.do")
	public ResBody list(String queryCondition) {
		ResBody resBody = null;
		try {
			JSONObject jsonObj = JSONObject.parseObject(queryCondition);
			int currentPage = jsonObj.getIntValue("currentPage");
			int pageSize = jsonObj.getIntValue("pageSize");
			EnumType enumType = jsonObj.getObject("data", EnumType.class);

			IPage<EnumType> page = new Page<EnumType>(currentPage, pageSize);

			QueryWrapper<EnumType> queryWrapper = new QueryWrapper<EnumType>(enumType);
			IPage<EnumType> enumTypes = enumTypeService.page(page, queryWrapper);

			resBody = ResBody.success(JSONObject.toJSON(enumTypes));
		} catch (Exception e) {
			e.printStackTrace();
			resBody = ResBody.failure().code(ResCodeEnum.PARAM_ERROR.getCode());
		}
		return resBody;
	}

	/**
	 * 
	 * @return
	 */
	@ApiOperation(value = "获取所有字段枚举类型", produces = "application/json")
	@GetMapping("/listAll.do")
	public ResBody listAll() {
		ResBody resBody = null;
		try {
			List<EnumType> enumTypes = enumTypeService.list();

			resBody = ResBody.success(JSONObject.toJSON(enumTypes));
		} catch (Exception e) {
			resBody = ResBody.failure().code(ResCodeEnum.PARAM_ERROR.getCode());
		}
		return resBody;
	}
}
